#include "includes/skybox.h"
#include "includes/util.h"

void skybox_init(Skybox * skybox){
	
	skybox->list 		= glGenLists(1);
	skybox->texture 	= util_load_texture("./images/see/skybox.png");
	
	glNewList(skybox->list, GL_COMPILE);
	
		/* Sol */
		glBegin(GL_QUADS);
			glTexCoord2f(0.25, 0.0);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.25, 1./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.50, 1./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.50, 0.0);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
		glEnd();
		
		/* Ciel */
		glBegin(GL_QUADS);
			glTexCoord2f(0.25, 1.);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.25, 2./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.5, 2./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.5, 1.);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
		glEnd();
		
		/* Mur des x negatifs */
		glBegin(GL_QUADS);
			glTexCoord2f(0.0, 1./3.);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.25, 1./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.25, 2./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.0, 2./3.);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
		glEnd();
		
		/* Mur des x positifs */
		glBegin(GL_QUADS);
			glTexCoord2f(0.75, 1./3.);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.5, 1./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.5, 2./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.75, 2./3.);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
		glEnd();
		
		/* Mur des y negatifs */
		glBegin(GL_QUADS);
			glTexCoord2f(1., 1./3.);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1., 2./3.);glVertex3f( -SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.75, 2./3.);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.75, 1./3.);glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
		glEnd();
		
		/* Mur des y positifs */
		glBegin(GL_QUADS);
			glTexCoord2f(0.25, 1./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.25, 2./3.);glVertex3f( -SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.5, 2./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, SKYBOX_SIZE);
			glTexCoord2f(0.5, 1./3.);glVertex3f( SKYBOX_SIZE, SKYBOX_SIZE, -SKYBOX_SIZE);
		glEnd();
		
	glEndList();
}

void skybox_draw(Skybox * skybox){
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, skybox->texture);
	
	glCallList(skybox->list);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
}